<?php

namespace app\model;

use app\stateData;
use think\facade\Log;
use think\Model;

class CluePersonCertificate extends Model
{
    /**
     * 人才证书表
     */

    protected $createTime='create_date';


    //关联证书表查询证书专业
    public function speciality(): \think\model\relation\HasOne
    {
        return $this->hasOne(SystemCertificate::class,'id','certificate_speciality');
    }
    //关联证书表查询证书类别
    public function certificateType(): \think\model\relation\HasOne
    {
        return $this->hasOne(SystemCertificate::class,'id','certificate_type');
    }
    public function certificateId(): \think\model\relation\HasOne
    {
        return $this->hasOne(SystemCertificate::class,'id','certificate_id')->bind (['certificate_name']);
    }

    public function getRegisterAttr( $name)
    {
        return [
            'id'=>$name,
            'text'=>stateData::$_zc_status[$name]
        ];
    }
    public function getCertificatePeriodOfValidityAttr($name)
    {
        if ($name == '0000-00-00 00:00:00') return  "";
        return $name;
    }

    public function getCertificateIdAttr( $name,$data)
    {
        $leixing = (new SystemCertificate)->getWhereIdCertificateModel($data['certificate_leixing'])?:'';
        $leixingId = $data['certificate_leixing']?:'';
        $leibie = (new SystemCertificate)->getWhereIdCertificateModel($data['certificate_leibie'])?:'';
        $leibieId = $data['certificate_leibie']?:'';
        $dengji = (new SystemCertificate)->getWhereIdCertificateModel($data['certificate_dengji'])?:'';
        $dengjiId = $data['certificate_dengji']?:'';
        $zhuanye = (new SystemCertificate)->getWhereIdCertificateModel($data['certificate_zhuanye'])?:'';
        $zhuanyeId = $data['certificate_zhuanye']?:'';
        $str = '';
        if ($leixing) $str .= $leixing.'/';
        if ($leibie) $str .= $leibie.'/';
        if ($dengji) $str .= $dengji.'/';
        if ($zhuanye) $str .= $zhuanye;

        $id = [];
        if ($leixingId) $id[] = $leixingId;
        if ($leibieId) $id[] = $leibieId;
        if ($dengjiId) $id[] = $dengjiId;
        if ($zhuanyeId) $id[] = $zhuanyeId;

        return [
            'id'=>$id,
            'text'=>rtrim($str,'/')
        ];
    }

    /**
     * 批量添加人才其他证书
     * @param $data
     * @return \think\Collection
     * @throws \Exception
     */
    public function addAllPersonCertificateModel($data): \think\Collection
    {
        return $this->saveAll ($data);
    }

    /**
     * 单个添加人才证书
     * @param $data
     * @return \app\model\CluePersonCertificate|\think\Model
     */
    public function addPersonCertificateModel($data){
        return self::create ($data);
    }

    /**
     * 查询该人才其他证书
     * @param $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonCertifiModel($id):array
    {
        return $this->where (['clue_p_id'=>$id])->select ()->toArray ();
    }

    /**
     * 修改证书数据
     * @param $data
     * @return \app\model\CluePersonCertificate
     */
    public function editCertificate($data)
    {
        return self::update ($data);
    }

    public function getJoinCertificateData($id): array
    {
        return $this->with (['certificateType','speciality'])->select()->toArray();
    }

    public function delPersonCertificateModel($id): bool
    {
        return self::destroy($id);
    }
}